<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */



class Action
{
	
	function check()
	{
		error_reporting(0);
		Cipher::setSalt('test');//密钥
		$sn = _POST('sn');
		$sn_info = _POST('sn_info');
		
		$sn = Cipher::decrypt($sn);
		$sn_info = Cipher::decrypt($sn_info);
		$post_type = Cipher::decrypt(_POST('post_type'));
		$data = _POST('data');
		
		$post = array();
		$post = json_decode(Cipher::decrypt($data), true);	
		
		$out = array('next_sync'=>$post['sync_time']+60, 'rm_site'=>0, 'rand'=>$post['rand'], 'sn'=>$sn);
		
		// @file_put_contents('./test_data.txt', var_export($sn, true));
		
		switch ($post_type) {
			case '2'://注册激活
					if ($sn) {
						$info = _model('sn')->read(array('sn_key'=>$sn));
						if ($info && $info['status'] != 0 && $info['status'] != 2) {
							_model('register_info')->create(array('sn'=>$sn, 'c_info'=>$sn_info, 'system_info'=>serialize($post), 'add_time'=>time()));
							_model('sn')->update(array('sn_key'=>$sn), array('status'=>2));
							_model('sn_mac')->create(array('sn'=>$sn, 'mac'=>$post['mac']));
							$out['sn_status'] = 1;
						} else {
							$out['sn_status'] = 0;
						}
					} else {
						$out['sn_status'] = 0;
					}
				break;
			case '3'://为取消绑定
				if ($sn) {
					_model('sn_mac')->delete(array('sn'=>$sn));
					_model('sn')->update(array('sn_key'=>$sn), array('status'=>3));
				} 
				$out['sn_status'] = 0;
				
				break;
			default:
				$info = _model('sn_mac')->read(array('sn'=>$sn));
				if ($info['mac'] == $post['mac']) {
					$out['sn_status'] = 1;
				} else {
					$out['sn_status'] = 0;
				}
				break;
		}
		
			
		
	echo  Cipher::encrypt(json_encode($out));
		
	}
	
	function index()
	{
		echo 'w';
	}
}


?>